/*
 * @Author: zhang zhijiang 2824735304@qq.com
 * @Date: 2025-04-24 17:18:56
 * @LastEditors: zhang zhijiang 2824735304@qq.com
 * @LastEditTime: 2025-04-26 15:27:28
 * @FilePath: /linux/tcpcalculator/tcpcal.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include"protocol.hpp"
#include"sock.hpp"
#include<time.h>
#include<cstring>
using namespace std;
//./tcpcal 127.0.0.1 8888
void usage(std::string proc)
{
    cout<<"usage:"<<proc<<"ip "<<"port"<<endl;
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        usage(argv[1]);
        exit(1);
    }
    srand(time(nullptr));
    std::string clientip = argv[1] ;
    uint16_t clientport = std::stoi(argv[2]);
    sock Sock;
    Sock.Init();
    bool r = Sock.Connect(clientip,clientport);
    if(!r) return -1;
    cout<<"success connect"<<endl;
    int cnt=10;
    std::string  opers = "+-*/%=&";
    while(cnt)
    {
        request re;
        re.x = rand()%3+1;
        re.y = rand()%3 + 1;
        re.op = opers[rand()%opers.size()];
        std::string content;
        re.serialize(content);
        content =  Encode(content);
        cout << content;
        ssize_t n = write(Sock.sockfd_,content.c_str(),content.size());
        char buffer[128];
        n = read(Sock.sockfd_,buffer,sizeof(buffer));
        if(n>0)
        {
            buffer[n] = 0;
            std::string package = buffer;
            content = "";
            Decode(package,content);
            response rp;
            rp.deserialize(content);
            cout<<rp.result<<"  "<<rp.code<<endl;
            cnt--;
        }

    }
    return 0;
}